package de.davvid.contracts;

import java.util.Enumeration;
import javax.microedition.lcdui.*;

import de.davvid.core.TimeRecordsMIDlet;

public class ContractListMenu extends Form implements CommandListener {

	private final Command create, delete, back;
	private final TimeRecordsMIDlet midlet;

	public ContractListMenu(TimeRecordsMIDlet midlet) {
		super("Contracts");

		this.midlet = midlet;

		// Create Commands
		create = new Command("New","Create new contract",Command.SCREEN, 0);
		back = new Command("Back",Command.BACK, 1);
		delete = new Command("Delete","Delete a contract",Command.SCREEN, 3);
		addCommand(create);
		addCommand(back);
		addCommand(delete);
		setCommandListener(this);
		
		int i=midlet.contractDB.size();
		for(Enumeration e = midlet.contractDB.getContracts(); e.hasMoreElements();) {
			Contract c = (Contract)e.nextElement();
			
			String msg = TimeRecordsMIDlet.dateToString(c.getBeginDate(), DateField.DATE) + " bis\n" +
				TimeRecordsMIDlet.dateToString(c.getEndDate(), DateField.DATE) +"("+c.getHours() + " h)";
			
			append(new StringItem("Contract "+(i--),msg));
		}
	}

	public void commandAction(Command c, Displayable d) {
		if(c == create) {
			midlet.showCreateContractMenu();

		} else if( c == delete) {
			midlet.showDeleteContractMenu();

		} else {
			midlet.showMainMenu();
		}

	}

}
